package com.zzuli.yxypre.controller;

import com.zzuli.yxypre.comment.R;
import com.zzuli.yxypre.entity.Comment;
import com.zzuli.yxypre.service.AlbumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @auther pony
 * @create 2023-04-28 15:22
 */
@RestController
@RequestMapping("album")
@CrossOrigin(origins = "*")
public class AlbumController {

    @Autowired
    private AlbumService albumService;

    /**
     * 1、根据 专辑（album） id 获取一张 专辑（album） 信息
     * 2、封装 专辑 评论数
     * @param id
     * @return
     */
    @GetMapping("getOneById/{id}")
    public R getOneById(@PathVariable("id") Integer id, HttpServletRequest request){
        return albumService.getOneById(id, request);
    }

    /**
     * 获取专辑的分页评论数据
     * @param order 排序关键字
     * @param albumId 专辑id
     * @param pageCurrent 当前页
     * @param pageSize 每页大小
     * @return
     */
    @GetMapping("/commentPage/{order}/{albumId}/{pageCurrent}/{pageSize}")
    public R commentPage(@PathVariable("order") String order, @PathVariable("albumId") Integer albumId, @PathVariable("pageCurrent") Integer pageCurrent,
                        @PathVariable("pageSize") Integer pageSize, HttpServletRequest request){
        System.out.println(albumId);
        System.out.println(pageCurrent);
        System.out.println(pageSize);
        return albumService.commentPage(order, albumId, pageCurrent, pageSize, request);
    }

    /**
     * 为专辑，添加评论
     * @param comment
     * @param request
     * @return
     */
    @PostMapping("/addComment")
    public R addComment(@RequestBody Comment comment, HttpServletRequest request){
        System.out.println(comment);
        // 判断评论内容是否为空
        if ("".equals(comment.getContent())||comment.getContent()==null){
            return null;
        }
        return albumService.addComment(comment, request);
    }

    /**
     * 为专辑评论点赞
     * @param commentId 评论id
     * @param flag 1：点赞 0：取消点赞
     * @param request
     * @return
     */
    @PutMapping("/thumbs/{commentId}/{flag}")
    public R thumbs(@PathVariable("commentId") Integer commentId, @PathVariable("flag") Integer flag,  HttpServletRequest request){
        return albumService.thumbs(commentId, flag, request);
    }

    /**
     * @param albumId 专辑id
     * @param count 初始加载评论数
     * @param request
     * @return
     */
    @GetMapping("/test/{albumId}/{count}")
    public R test(@PathVariable("albumId") Integer albumId, @PathVariable("count") int count, HttpServletRequest request){
        return albumService.test(albumId, count, request);
    }
}
